#include "lvgl.h"
#include "font/font.h"
#include "esp_log.h"
#include <stdio.h>
#include "app.h"

/* 👇👇👇👇👇👇 新增页面 👇👇👇👇👇👇 */
extern app_s afe_info_page;
extern app_s afe_info2_page;
extern app_s bat_info_page;
extern app_s coulomb_counte_info1_page;
extern app_s coulomb_counte_info2_page;
extern app_s afe_state_page;
extern app_s config_page;

static app_t app_pages[] = {
    &config_page,
    &afe_state_page,
    &afe_info_page,
    &afe_info2_page,
    &bat_info_page,
    &coulomb_counte_info1_page,
    &coulomb_counte_info2_page,
};
#define APP_PAGE_COUNT (sizeof(app_pages) / sizeof(app_pages[0]))
/* 👆👆👆👆👆👆 新增页面 👆👆👆👆👆👆 */

static uint8_t cur_page_idx = 0;
static bool enter_sub_page = false;
// static bool page_on_scroll = false;
static uint32_t last_enter_tick = 0;
static lv_timer_t *app_timers[APP_PAGE_COUNT] = {NULL};

static void change_page(lv_obj_t *app, uint8_t idx)
{
    app_t cur_app = app_pages[cur_page_idx];
    lv_timer_t *app_timer = app_timers[cur_page_idx];
    if (cur_app->level)
        cur_app->level(app_timer, cur_app->user_data);
    lv_timer_resume(app_timer);

    // for (uint8_t i = 0; i < APP_PAGE_COUNT; i++)
    //     lv_timer_pause(app_timers[i]);

    app_t next_app = app_pages[idx];
    lv_timer_t *next_timer = app_timers[idx];
    if (next_app->enter)
        next_app->enter(next_timer, next_app->user_data);
    if (next_timer)
        lv_timer_resume(next_timer);

    // page_on_scroll = true;
    lv_obj_set_tile_id(app, idx, 0, 1);
    cur_page_idx = idx;
}

static void app_container_event_cb(lv_event_t *e)
{
    // if (e->code == LV_EVENT_SCROLL_BEGIN)
    // {
    //     page_on_scroll = true;
    //     return;
    // }
    // else if (e->code == LV_EVENT_SCROLL_END)
    // {
    //     page_on_scroll = false;
    //     return;
    // }
    // if (page_on_scroll)
    //     return;
    lv_obj_t *app_container = lv_event_get_user_data(e);
    char c = lv_indev_get_key(lv_indev_get_act());

    app_t app = app_pages[cur_page_idx];
    lv_timer_t *app_timer = app_timers[cur_page_idx];

    if (c == LV_KEY_DOWN) /* 单击 - 选项切换 / 下一页面 */
    {
        if (enter_sub_page)
        {
            if (!app->update || !app->update(app_timer, app->user_data, APP_EVENT_TYPE_NEXT))
            {
            }
        }
        else
        {
            uint8_t idx = cur_page_idx + 1;
            if (idx >= APP_PAGE_COUNT)
                idx = 0;
            if (idx != cur_page_idx)
                change_page(app_container, idx);
        }
    }
    else if (c == LV_KEY_ENTER) /* 双击 - 选项确认 / 上一页面 */
    {
        if (enter_sub_page)
        {
            if (!app->update || !app->update(app_timer, app->user_data, APP_EVENT_TYPE_CLICK))
            {
            }
        }
        else
        {
            int8_t idx = cur_page_idx - 1;
            if (idx < 0)
                idx = APP_PAGE_COUNT - 1;
            if (idx != cur_page_idx)
                change_page(app_container, idx);
        }
    }
    else if (c == LV_KEY_HOME) /* 长按 - 进入下一级菜单 / 调出确认框 */
    {
        if (lv_tick_elaps(last_enter_tick) > 3000)
        {
            if (app->update && app->update(app_timer, app->user_data, APP_EVENT_TYPE_CONFIRM))
                enter_sub_page = true;
            else
                enter_sub_page = false;
            last_enter_tick = lv_tick_get();
        }
    }
}

static void timer_task_cb(struct _lv_timer_t *timer)
{
    uint8_t idx = cur_page_idx + 1;
    if (idx >= APP_PAGE_COUNT)
        idx = 0;
    if (idx != cur_page_idx)
        change_page(timer->user_data, idx);
}

void ui_init(void)
{
    font_init();

    lv_obj_t *obj = lv_tileview_create(lv_scr_act());
    lv_obj_set_size(obj, lv_pct(100), lv_pct(100));
    lv_obj_set_scrollbar_mode(obj, LV_SCROLLBAR_MODE_OFF);
    lv_obj_add_flag(obj, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_add_event_cb(obj, app_container_event_cb, LV_EVENT_KEY, obj);
    // lv_obj_add_event_cb(obj, app_container_event_cb, LV_EVENT_SCROLL_END, obj);
    // lv_obj_add_event_cb(obj, app_container_event_cb, LV_EVENT_SCROLL_BEGIN, obj);
    lv_obj_set_style_bg_color(obj, lv_color_make(0, 0, 0), LV_PART_MAIN);

    lv_group_t *g = lv_group_get_default();
    lv_group_add_obj(g, obj);

    for (uint8_t i = 0; i < APP_PAGE_COUNT; i++)
        app_timers[i] = app_pages[i]->create(obj, i);

    change_page(obj, 2);
    // change_page(obj, APP_PAGE_COUNT - 1);

    // lv_timer_t *timer = lv_timer_create(timer_task_cb, 3000, obj);
}